<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/8
 * Time: 14:38
 */

namespace Admin\Controller;
use Admin\Controller\AdminController;
use Admin\Model\CommunicateModel;
class CommunicateController extends AdminController
{
    //党员互动文章列表$this->checkPower();
    public function index ()
    {
        $this->checkPower();
        //根据作者名搜索
        $name = "";
        if(!empty($_GET['name'])){
            $name = I("get.name");
        }

        $sort= "";
        if(!empty($_GET['sort'])){
            $sort = I("get.sort");
        }

        //根据文章发布时间搜索
        $time= "";
        if(!empty($_GET['time'])){
            $time = I("get.time");
        }
        //根据文章是否显示搜索
        $is_show = "";
        if(!empty($_GET['is_show'])){
            $is_show = I("get.is_show");
        }
        $data['name'] = $name;
        $data['sort'] = $sort;
        $data['time'] = $time;
        $data['is_show'] = $is_show;

        //排序字段
        $order = "";
        if(!empty($_GET['order'])){
            $order = I("get.order");
        }
        //排序方式
        $order1 = "";
        if(!empty($_GET['order1'])){
            $order1 = I("get.order1");
        }
        $data['order'] = $order;
        $data['order1'] = $order1;
        $this->title = "文章列表";

        $com = new CommunicateModel();
        $res = $com -> queryAllCom($data);
        $this->comList = $res['list'];
        $this->page = $res['show'];

        //查询分类
        $sort = M("communicate_sort");
        $sortList = $sort ->field("id,name,is_use") -> select();
        $this -> assign("sortList",$sortList);

        $this->display("Communicate/index");
    }

    //根据文章id查看文章详情
    public function queryArtInfo ()
    {
        $id = I("get.id");
        $this->title = "文章详情";
        $art = new CommunicateModel();
        $info = $art->queryArtInfoById($id);
        $this->info = $info;
        $this->assign("prev",$_SERVER['HTTP_REFERER']);
        $this->display("Communicate/detail");
    }

    //党员惠东文章分类列表
    public function cateList ()
    {
        $this->title="文章分类";
        //查询分类
        $sort = M("communicate_sort");
        $sortList = $sort -> select();
        $this -> assign("sortList",$sortList);
        $this->display("Communicate/sort");
    }

    //更改分类是否可用
    public function isUse()
    {
        $id = I("get.id");
        $is_use = I("get.use") == 0 ? 1 : 0;
        $sort = M("communicate_sort");
        $data['is_use'] = $is_use;
        $sort->where("id=$id")->save($data);
        $this->redirect("Communicate/cateList");
    }

    //编辑分类名
    public function editSort()
    {
        $this->checkPower();
        $name =  $_GET['name'];
        $id = $_GET['id'];
        $sort = M("communicate_sort");
        $data['name'] = $name;
        echo $sort->where("id=$id")->save($data);
    }

    //分类名
    public function addSort()
    {
        $this->checkPower();
        $name =  $_GET['name'];
        $sort = M("communicate_sort");
        $data['name'] = $name;
        $data['create_time'] = date("Y-m-d H:i:s");
        echo $sort->add($data);
    }

    //党员互动交流文章是否显示$this->checkPower();
    public function isShow ()
    {
//        $this->checkPower();
        $id = I("get.id");
        $is_show = I("get.show") == 0 ? 1 : 0;
        $art = new CommunicateModel();
        $art->updateShow($id,$is_show);
        $this->redirect("Communicate/index");
    }
}